
$(document).ready(function() {
    bindEvent();
    InputCheck.checkInputValueIsNumber("#input_amount_phone");

});


function bindEvent() {
    submitImageVerificationCode();
    clickPhoneVerification();
    submitMessageVirificationCode();
    submitNewPassword();
    clickSendPhoneVerificationCode();
    login();
    changeImageVertificationCode();
}

/* 点击填写帐号阶段的 下一步  */
function submitImageVerificationCode() {
    $("#submit_amount").click(function() {
        postAmountPhoneAndVerificationCode();
    });
}

/* 点击验证身份阶段的手机 立即验证 */
function clickPhoneVerification() {
    $("#verify_button").click(function () {
        hideAndshowModule(".verifyIdentity", ".getVeryficationCode");
        changeElementBackColor("second");
    })
}

/* 点击重置密码阶段 验证身份的 下一步 */
function submitMessageVirificationCode() {
    $("#confirmGetVeryficationCode").click(function () {
        if ($("#messageCodeInput").val() == "") {
            alert("请填写验证码");
        } else {
            hideAndshowModule(".getVeryficationCode", ".resetPassword");
            changeElementBackColor("third")
        }
    })
}

// function judgeVerificationCode() {
//     var phone = $("#input_amount_phone").val();
//     var verifyCode = $("#messageCodeInput").val();
//
//     Api.verifyPhone(phone, verifyCode, function (result) {
//         console.log(result);
//     }, function (error) {
//         console.log(error);
//     })
// }

/* 点击充值密码阶段 输入密码的 确定 */
function submitNewPassword() {
    $("#submit_password_input").click(function () {
        if ($("#newPassword").val() != $("#newPasswordConfirm").val()) {
            alert("两次密码不相同")
        } else {
            // post 提交新密码
            postNewPassword();

        }
    })
}


/* 调用忘记密码接口 */
function postNewPassword() {
    var phone = $("#input_amount_phone").val();
    var newPassword = $("#newPassword").val();
    var vcode = $("#messageCodeInput").val();
    Api.setupForgetPassword(phone, newPassword, vcode, function (result) {
        if (result.code == 200) {
            hideAndshowModule(".resetPassword", ".success");
            changeElementBackColor("fouth");
        } else {
            if (result.code == 404) {
                alert('手机号和验证码不匹配');
            } else if (result.code == 401) {
                alert('手机号或验证码不正确');
            } else if (result.code == 400) {
                alert('手机号格式不正确');
            }
            window.location.reload();
        }

    }, function (error) {
        alert("修改失败");
    })
}


/* 点击发送短信验证码 */
function clickSendPhoneVerificationCode() {
    $("#sendVerificationCode").click(function () {
        var phone = $("#input_amount_phone").val();
        sendPhoneVerifyCode(phone);
    })
}

/* 调用发送短信验证码接口，调用成功则开始倒计时 */
function sendPhoneVerifyCode(phone) {
    Api.fetchVerificationCode(phone, function (result) {
        var time = 60;
        $("#sendVerificationCode").attr("disabled",true);
        var countDown = setInterval(function () {
            $("#sendVerificationCode").val(time);
            if (time == 0) {
                initVerificationCode(countDown);
            }
            time--;
        }, 1000);
    }, function (error) {
        alert("发送验证码失败");
    })
}


/* 初始化短信验证码 */
function initVerificationCode(interval) {
    $("#sendVerificationCode").val("获取验证码");
    $("#sendVerificationCode").attr("disabled",false);
    window.clearInterval(interval);
}


/* 点击更改图片验证码 */
function changeImageVertificationCode() {
    $(".change_code").click(function () {
        setupImageVeritification();
    });
}

/* 点击登录按钮 */
function login() {
    $(".login").click(function() {
        window.location.href = "../html/login.html";
    })
}

/* 调用获取图片验证码接口 */
function setupImageVeritification() {
    //post   修改验证码图片
    $(".verification_code_img").attr("src", "");
}

/* 判断填写帐号阶段 填写信息是否正确 */
function postAmountPhoneAndVerificationCode() {
    var phoneNumer = $("#input_amount_phone").val();
    var codeNumer = $("#input_amount_code").val();
    if (phoneNumer === "" || codeNumer == "") {
        alert("请填写完整信息");
    } else {
        if (InputCheck.checkInputValueIsPhone("#input_amount_phone")) {
            hideAndshowModule(".inputAmount", ".verifyIdentity");
            changeElementBackColor("first");
            $(".verify_phone_numer").text(phoneNumer);
        } else {
            alert("手机格式不正确");
        }
    }
}


/* 修改进度条的样式 */
function changeElementBackColor(model) {
    $("." + model + "_left_line").addClass('complete_line');
    $("." + model).addClass('complete_line');
    $("." + model + "_right_line").addClass('complete_line');
}


/* 隐藏显示模块
* hideModule 需要隐藏的模块
* showModule 需要显示的模块
* */
function hideAndshowModule(hideModule, showModule) {
    $(hideModule).hide();
    $(showModule).show();
}


